<?php

namespace App\Admin\Controllers;

use App\Models\MoneyRecord;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class MoneyRecordController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'MoneyRecord';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new MoneyRecord());

        $grid->column('id', __('Id'));
        $grid->column('uid', __('Uid'));
        $grid->column('money', __('金额'));
        $grid->column('status', __('状态'))->using(['1' => '等待打款', '2' => '已同意','-1'=>'已拒绝']);
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(MoneyRecord::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('uid', __('Uid'));
        $show->field('money', __('Money'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new MoneyRecord());

        $form->number('uid', __('Uid'));
        $form->decimal('money', __('Money'));;
        $form->select('status', __('Status'))->default(1)->options(['1' => '等待打款', '2' => '已同意','-1'=>'已拒绝']);
        $form->saved(function (Form $form) {
            if($form->status == -1){
                User::where('id',$form->uid)->increment('money',$form->money);
            }
        });
        return $form;
    }
}
